/**
 * 
 */
package com.alstudio.ui.module.main.item;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alstudio.view.ViewUtils;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class MainBottomItem extends RelativeLayout {

	// 底部色织
	private View item_bottom_line;
	// 文字说明
	private TextView item_txt;
	// 图标
	private View item_img;
	// 未读消息数
	private TextView badge;

	LayoutInflater inflater;

	private int comImgRes = -1, selectedImgRes = -1;

	//
	private int badgeValue = 0;

	/**
	 * @param context
	 */
	public MainBottomItem(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.home_bottom_item, this);
		initView();
	}

	private void initView() {
		item_bottom_line = findViewById(R.id.item_bottom_line);
		item_txt = (TextView) findViewById(R.id.item_txt);
		item_img = findViewById(R.id.item_img);
		badge = (TextView) findViewById(R.id.badge);
		resetBadgeValue();
	}

	public void setBadgeBg(int res) {
		badge.setBackgroundResource(res);
	}

	public void resetBadgeValue() {
		badgeValue = 0;
		checkBadgeShowState();
	}

	public void setBadgeValue(int value) {
		badgeValue = value;
		String badgeTmp = "" + badgeValue;
		if (badgeValue > 99) {
			badgeTmp = "99+";
		}
		badge.setText(badgeTmp);
		checkBadgeShowState();
	}

	public void increment() {
		badge.setText((++badgeValue) + "");
		checkBadgeShowState();
	}

	public void decrement() {
		if (badgeValue > 0) {
			badge.setText((--badgeValue) + "");
		}
		checkBadgeShowState();
	}

	public void setSelectImgRes(int com, int selected) {
		comImgRes = com;
		selectedImgRes = selected;
	}

	public void setItemSelected(boolean selected) {
		item_bottom_line.setSelected(selected);
		item_txt.setSelected(selected);
		item_img.setSelected(selected);
		if (selected) {
			// 选中
			item_txt.setTextColor(getResources().getColor(
					R.color.main_bottom_item_txt_selected_color));
			item_bottom_line.setBackgroundColor(getResources().getColor(
					R.color.main_bottom_item_txt_selected_color));
			if (selectedImgRes != -1)
				item_img.setBackgroundResource(selectedImgRes);
		} else {
			// 未选中
			item_txt.setTextColor(getResources().getColor(
					R.color.main_bottom_item_txt_color));
			item_bottom_line.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			if (comImgRes != -1)
				item_img.setBackgroundResource(comImgRes);

		}
	}

	public void setItemText(String txt) {
		item_txt.setText(txt);
	}

	public void setItemText(int txt) {
		item_txt.setText(txt);
	}

	public void setItemTextColor(int color) {
		item_txt.setTextColor(color);
	}

	public void setItemImgRes(int res) {
		item_img.setBackgroundResource(res);
	}

	public void setItemBottomLineRes(int res) {
		item_bottom_line.setBackgroundResource(res);
	}

	/**
	 * @return the item_img
	 */
	public View getItem_img() {
		return item_img;
	}

	private void checkBadgeShowState() {
		if (badgeValue > 0) {
			ViewUtils.showView(badge);
		} else {
			ViewUtils.goneView(badge);
		}
	}
}
